import 'package:flutter/material.dart';
import 'package:ost_flutter_module/class/entity/store/store_search_entity.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/defines_macro.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/help/ost_route.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_radius_container.dart';
import 'package:ost_flutter_module/widget/custom_widget/image_text_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/text_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/wrap_tag.dart';

import 'dart:convert' as convert;

class StoreCourseHomeCard extends StatelessWidget {
  final CourseCardType type;
  final bool isShopCart; // 是否是购物车商品，此状态下不显示报名按钮，显示价格
  final StoreSearchEntityShiftList shift;
  final ValueChanged<int> onTap;

  const StoreCourseHomeCard(
      {Key key, this.type, this.isShopCart = false, this.onTap, this.shift})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GContainer(
      child: CourseCardWidget(type: type, isShopCart: isShopCart, shift: shift),
      onTap: () => onTap(1),
    );
  }
}

class CourseCardWidget extends StatelessWidget {
  final StoreSearchEntityShiftList shift;
  final String image, title, payedIcon, payedNum, buttonText;
  final CourseCardType type;
  final bool isShopCart;
  final ValueChanged<int> onTap;

  const CourseCardWidget({
    Key key,
    this.image,
    this.title,
    this.payedIcon,
    this.payedNum,
    this.onTap,
    this.buttonText,
    this.type = CourseCardType.recommend,
    this.isShopCart = false,
    this.shift,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return shift == null
        ? Container()
        : Container(
            child: Padding(
              padding: EdgeInsets.all(Adapt.px(10)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.network(
                      shift.pic != null && shift.pic.isNotEmpty
                          ? shift.pic
                          : default_image,
                      width: 88,
                      height: 75,
                      fit: BoxFit.cover),
                  SizedBox(width: Adapt.px(8)),
                  _buildCard(type: type),
                ],
              ),
            ),
          );
  }

  _buildCard({CourseCardType type}) {
    if (type == CourseCardType.recommend) return _recommend(isShopCart);
    if (type == CourseCardType.professor) return _professor(isShopCart);
    if (type == CourseCardType.teacher) return _teacher(isShopCart);
    if (type == CourseCardType.parent) return _parent(isShopCart);
    if (type == CourseCardType.relate) return _relate();

    return Container();
  }

  _recommend(bool isShopCart) => Expanded(
        child: Row(
          children: [
            Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(shift.name ?? '', style: TextStyles.titleStyle),
                Text(_getCourseSkuName(shift.skuName),
                    style: TextStyles.detailTitleStyle),
                SizedBox(height: Adapt.px(4)),
                WrapTag(titles: shift.tagNames == null ? [] : shift.tagNames),
              ],
            ),
            SizedBox(width: Adapt.px(8)),
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  CourseCardPriceWidget(
                      originalPrice:
                          '￥${shift.purchasemoney.toStringAsFixed(2)}',
                      price: '￥${shift.originalmoney.toStringAsFixed(2)}'),
                  SizedBox(height: Adapt.px(10)),
                  isShopCart
                      ? Expanded(
                          child: Text(
                          '￥1800',
                          style: TextStyles.priceTitleStyle,
                          textAlign: TextAlign.end,
                        ))
                      : CourseCardSignupWidget(),
                ],
              ),
            )
          ],
        ),
      );

  _professor(bool isShopCart) => Expanded(
        child: Row(
          children: [
            Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title ?? '发音口及专家可', style: TextStyles.titleStyle),
                SizedBox(height: Adapt.px(4)),
                Container(height: Adapt.px(22)),
                Text(title ?? '1次', style: TextStyles.detailTitleStyle),
              ],
            ),
            SizedBox(width: Adapt.px(8)),
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  CourseCardPriceWidget(),
                  SizedBox(height: Adapt.px(10)),
                  CourseCardSignupWidget(),
                ],
              ),
            )
          ],
        ),
      );

  _teacher(bool isShopCart) => Expanded(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(title ?? '发音口及专家可', style: TextStyles.titleStyle),
            SizedBox(height: Adapt.px(4)),
            Container(height: Adapt.px(22)),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GImageTextButton(
                  '1837',
                  height: 20,
                  textLeadingPadding: 4,
                  icon: Icons.person_outline_rounded,
                  iconSize: 16,
                  backgroundColor: Colors.transparent,
                ),
                CourseCardSignupWidget(),
              ],
            ),
          ],
        ),
      );

  _parent(bool isShopCart) => Expanded(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(title ?? '儿童家庭训练宝藏干货视频课', style: TextStyles.titleStyle),
            Text(title ?? '18次', style: TextStyles.detailTitleStyle),
            SizedBox(height: Adapt.px(4)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                GImageTextButton(
                  '1837',
                  height: 20,
                  textLeadingPadding: 4,
                  icon: Icons.person_outline_rounded,
                  iconSize: 16,
                  backgroundColor: Colors.transparent,
                ),
                Expanded(
                    child: Text(
                  '￥1800',
                  style: TextStyles.priceTitleStyle,
                  textAlign: TextAlign.end,
                )),
                SizedBox(width: Adapt.px(8)),
                CourseCardSignupWidget(),
              ],
            ),
          ],
        ),
      );

  _relate() => Expanded(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title ?? '认知幻灯手电筒', style: TextStyles.titleStyle),
                Text('￥1800',
                    style: TextStyles.priceTitleStyle,
                    textAlign: TextAlign.end),
              ],
            ),
            SizedBox(height: Adapt.px(4)),
            Container(height: Adapt.px(22)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title ?? 'x1', style: TextStyles.detailTitleStyle),
                SizedBox(width: Adapt.px(8)),
                Container(
                  height: 30,
                  width: 30,
                  child: GTextButton(
                    padding: 0,
                    child: Icon(Icons.check_circle, color: colorDCDCDC),
                  ),
                ),
              ],
            ),
          ],
        ),
      );

  // 拼接后台返回的标签，
  // 如果是课程，取 key 为 '节数' 对应的 value， 拼接 '${value}节'
  String _getCourseSkuName(String skuName) {
    if (skuName == null || skuName.isEmpty) return '';
    List<dynamic> skuNames = convert.jsonDecode(skuName);

    if (skuNames.isEmpty) return '';
    var _name = '';
    skuNames.forEach((element) {
      element.forEach((key, value) {
        if (key == '节数') {
          _name = value;
          return;
        }
      });
    });

    if (_name == '') return _name;
    return '$_name节';
  }
}

class CourseCardPriceWidget extends StatelessWidget {
  final String originalPrice, price;
  const CourseCardPriceWidget({Key key, this.originalPrice, this.price})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Text(originalPrice ?? '￥3000',
            style: TextStyles.originalPriceTitleStyle),
        SizedBox(height: Adapt.px(2)),
        Text(price ?? '￥1800', style: TextStyles.priceTitleStyle),
      ],
    );
  }
}

class CourseCardSignupWidget extends StatelessWidget {
  final String title;
  const CourseCardSignupWidget({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: Adapt.px(22),
      width: Adapt.px(44),
      child: GTextButton(
        title: title ?? '报名',
        padding: 0,
        isStadiumBorder: true,
        normalForgroundColor: color42270C,
        normalBackgroundColor: colorFACD33,
        onPressed: () {
          Navigator.pushNamed(context, GRouter.order_page);
        },
      ),
    );
  }
}
